﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Collections;
using System.Configuration;
using System.Globalization;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserControls
{
    public partial class UCUserGrid : System.Web.UI.UserControl
    {
        private int[] hideColumns;
        public static int pageNo = 0;
        public List<GridViewStyle> styleList;
        public List<string> alignCols = new List<string>();
        public bool IsLastPage = true;

        int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"].ToString());

        #region Public Properties

        public int[] HideColumns
        {
            get { return hideColumns; }
            set { Session["hideCols"] = value; }
        }


        #endregion



        protected void Page_Load(object sender, EventArgs e)
        {
            /*GridViewRow col = grdUsers.HeaderRow;
            if (grdUsers.HeaderRow != null)
            {
                TableCellCollection columns = grdUsers.HeaderRow.Cells;

                if (styleList != null)
                {
                    for (int i = 0; i < columns.Count; i++)
                    {
                        GridViewStyle style = styleList.SingleOrDefault(p => p.ColumnId.Contains(columns[i].Text));
                        if (style != null)
                        {
                            columns[i].Text = style.ColumnHeader;
                            if (style.ColumnWidth != -1)
                                columns[i].ControlStyle.Width = style.ColumnWidth;
                            if (style.Align != null)
                                alignCols.Add(i.ToString());
                        }
                    }
                }
            }*/
        }

        #region Set Data Source To Grid

        public void SetDataSource(DataTable tbl)
        {
            grdUsers.DataSource = tbl;
            grdUsers.DataBind();
        }

        public void SetDataSource(DataTable tbl, int[] hideIndex)
        {
            Session["hideCols"] = hideIndex;
            grdUsers.DataSource = tbl;
            grdUsers.DataBind();
        }

        public void SetDataSource(DataSet ds)
        {
            grdUsers.DataSource = ds;
            grdUsers.DataBind();
        }

        public void SetDataSource(DataSet ds, int[] hideIndex)
        {
            Session["hideCols"] = hideIndex;
            grdUsers.DataSource = ds;
            grdUsers.DataBind();
        }

        public void SetDataSource(ICollection coll)
        {
            grdUsers.DataSource = coll;
            grdUsers.DataBind();
        }

        public void SetDataSource(ICollection coll, int[] hideIndex)
        {
            Session["hideCols"] = hideIndex;
            grdUsers.DataSource = coll;
            grdUsers.DataBind();
        }

        #endregion




        public bool SetVisible
        {
            set
            {
                grdUsers.Visible = value;
            }
        }


        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            string pageUrl = Request.ServerVariables["URL"];

            foreach (GridViewRow row in grdUsers.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");

                if (rbtn != null)
                {
                    if ((rbtn.ClientID.ToString() == ((RadioButton)sender).ClientID.ToString()))
                    {
                        //Session[pageUrl + "view"] = row.Cells[2].Text;

                        if (this.Page is IDataPage)
                            ((IDataPage)this.Page).GetId(row);
                        rbtn.Checked = true;
                    }
                    else
                    {
                        rbtn.Checked = false;
                    }


                }


            }

        }


        protected void grdUsers_DataBound(object sender, EventArgs e)
        {
            int rw = grdUsers.Rows.Count;

            if (txtPageNo.Text != "1")
            {
                lbtPrev.Visible = true;
            }

            if (rw > 0)
            {
                GridViewRow firstRow = grdUsers.Rows[0];
                RadioButton rbtn = (RadioButton)firstRow.FindControl("RadioButton1");

                if (rbtn != null)
                {
                    rbtn.Checked = true;
                }


                CheckBox chk = (CheckBox)firstRow.Cells[0].FindControl("chkUsers");
                if (chk != null)
                {
                    chk.Checked = true;
                }

                int curr = 0;

                if (!string.IsNullOrEmpty(txtPageNo.Text))
                {
                    curr = Convert.ToInt32(txtPageNo.Text);
                }
                else
                {
                    curr = 1;
                }


                if (curr == 1)
                {

                    if (IsLastPage)
                    {
                        lbtNext.Visible = false;
                        lbtGo.Visible = false;
                        txtPageNo.Visible = false;
                        lbtPrev.Visible = false;
                    }
                    else
                    {
                        lbtNext.Visible = true;
                        lbtGo.Visible = true;
                        txtPageNo.Visible = true;
                        lbtPrev.Visible = false;
                    }



                }
                else if (IsLastPage)
                {
                    lbtNext.Visible = false;
                    lbtGo.Visible = true;
                    txtPageNo.Visible = true;
                    lbtPrev.Visible = true;
                }
                else
                {
                    lbtNext.Visible = true;
                    lbtGo.Visible = true;
                    txtPageNo.Visible = true;
                    lbtPrev.Visible = true;
                }


                Session["firstRow"] = grdUsers.Rows[0];







            }
            else
            {
                lbtNext.Visible = false;
                lbtPrev.Visible = false;
                lbtGo.Visible = false;
                txtPageNo.Visible = false;
            }

            /* if (grdUsers.Rows.Count > 0)
             {
                 TableCellCollection columns = grdUsers.HeaderRow.Cells;
               

                 if (styleList != null)
                 {
                     for (int i = 0; i < columns.Count; i++)
                     {
                         GridViewStyle style = styleList.SingleOrDefault(p => p.ColumnId.Contains(columns[i].Text));
                         if (style != null)
                         {
                             columns[i].Text = style.ColumnHeader;
                             if (style.ColumnWidth != -1)
                                 columns[i].ControlStyle.Width = style.ColumnWidth;
                             if (style.Align != null)
                                 alignCols.Add(i.ToString()); 

                         }
                     }
                 }
             }*/
            /*else
                MassageSummery("IIS504");*/
        }


        protected void grdUsers_RowCreated(object sender, GridViewRowEventArgs e)
        {
           // e.Row.Height = 2;

            int[] cols = (int[])Session["hideCols"];

            if (cols != null)
            {
                for (int i = 0; i < cols.Length; i++)
                {
                    e.Row.Cells[cols[i]].CssClass = "hiddencol";


                }
            }

            if (e.Row.RowType == DataControlRowType.DataRow && alignCols.Count > 0)
            {
                for (int i = 0; i < alignCols.Count; i++)
                {
                    e.Row.Cells[int.Parse(alignCols[i])].HorizontalAlign = HorizontalAlign.Right;
                }
            }
        }

        //Change time format in run time

        protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //foreach (TableCell cell in e.Row.Cells)
                //{                 
                //    try
                //    {
                //        //DateTime time = Convert.ToDateTime(cell.Text);
                //        //cell.Text = time.Day.ToString() + "/" + time.Month.ToString() + "/" + time.Year.ToString();

                //    }
                //    catch { }
                //}

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (grdUsers.HeaderRow.Cells[i].Text.Equals("Suspence", StringComparison.CurrentCultureIgnoreCase) || grdUsers.HeaderRow.Cells[i].Text.Equals("Premium", StringComparison.CurrentCultureIgnoreCase) || grdUsers.HeaderRow.Cells[i].Text.Equals("Premium(Rs)", StringComparison.CurrentCultureIgnoreCase) || grdUsers.HeaderRow.Cells[i].Text.Equals("Balance(Rs)", StringComparison.CurrentCultureIgnoreCase) || grdUsers.HeaderRow.Cells[i].Text.Equals("Collection Due(Rs)", StringComparison.CurrentCultureIgnoreCase))
                    {
                        string temp = e.Row.Cells[i].Text;                        
                        e.Row.Cells[i].Text = String.Format("{0:0,0.00}", float.Parse(temp));
                    }
                    if (grdUsers.HeaderRow.Cells[i].Text.Equals("Last Sync Date"))
                    {
                        string temp = e.Row.Cells[i].Text;
                        e.Row.Cells[i].Text = temp.Substring(0, 10);
                    }
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                TableCellCollection columns = e.Row.Cells;

                if (styleList != null)
                {
                    for (int i = 0; i < columns.Count; i++)
                    {
                        GridViewStyle style = styleList.SingleOrDefault(p => p.ColumnId.Equals(columns[i].Text));
                        if (style != null)
                        {
                            if(style.ColumnHeader != null)
                                columns[i].Text = style.ColumnHeader;
                            if (style.ColumnWidth != -1)
                                columns[i].ControlStyle.Width = style.ColumnWidth;
                            if (style.Align != null)
                                alignCols.Add(i.ToString());
                        }
                    }
                }
            }
        }

        protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdUsers.PageIndex = e.NewPageIndex;
            grdUsers.DataBind();
        }

        //protected void btnPrevious_Click(object sender, EventArgs e)
        //{
        //    if (this.Page is IDataPage)
        //        ((IntTest)this.Page).Previous();
        //}

        //protected void txtGo_Click(object sender, EventArgs e)
        //{
        //    if (this.Page is IDataPage)
        //        ((IntTest)this.Page).Go();

        //}

        //protected void btnNext_Click(object sender, EventArgs e)
        //{
        //    if (this.Page is IDataPage)
        //        ((IntTest)this.Page).Next();


        //}


        public void SetText(string text)
        {
            txtPageNo.Text = text;

        }


        public string GetText()
        {
            if (txtPageNo.Text.Trim() == "0" || txtPageNo.Text == "")
            {
                txtPageNo.Text = "1";
                return "1";
            }
            else
                return txtPageNo.Text;

        }

        protected void lbtPrev_Click(object sender, EventArgs e)
        {
            if (this.Page is IntTest)
                ((IntTest)this.Page).Previous();
            //Previous();
        }

        protected void lbtGo_Click(object sender, EventArgs e)
        {
            if (this.Page is IntTest)
                ((IntTest)this.Page).Go();
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            if (this.Page is IntTest)
                ((IntTest)this.Page).Next();

        }

        protected void grdUsers_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lbPrevious_Click(object sender, EventArgs e)
        {


        }

        internal void ResetDataSource()
        {
            grdUsers.DataSource = null;
            grdUsers.DataBind();
        }


    }



}

